<?php
namespace tencent;

use think\facade\Cache;
use TlsSigApi\TLSSigAPIv2;
use utils\Http;

class IMGroup{

    private $SDKAppID;
    private $secretKey;
    private $identifier;
    private $baseUrl = 'https://console.tim.qq.com/v4/group_open_http_svc/';

    public function __construct($SDKAppID, $secretKey, $identifier = 'administrator')
    {
        $this->SDKAppID = $SDKAppID;
        $this->secretKey = $secretKey;
        $this->identifier = $identifier;
    }

    private function getUserSig(){
        $key = 'tencent_im_group_user_sign_' . $this->identifier;
        $userSig = Cache::get($key);
        if (empty($userSig)){
            $tls = new TLSSigAPIv2($this->SDKAppID, $this->secretKey);
            $expire = 86400 * 180;
            $userSig = $tls->genUserSig($this->identifier, $expire);
            Cache::set($key, $userSig, $expire - 3600);
        }

        return $userSig;
    }

    private function getUrl($method){
        $url = $this->baseUrl . $method;
        $params['sdkappid'] = $this->SDKAppID;
        $params['identifier'] = $this->identifier;
        $params['usersig'] = $this->getUserSig();
        $params['random'] = mt_rand(0, 4294967295);
        $params['contenttype'] = 'json';
        return Http::getQueryUrl($url, $params);
    }

    public function getGroupList(){
        $url = $this->getUrl('get_appid_group_list');
        $res = Http::get($url);
        $res = json_decode($res, true);
        if ($res['ErrorCode'] === 0){
            return $res['GroupIdList'];
        }
        return $res;
    }

    public function createGroup($name, $ownerAccount, $group_id = 0, $type = 'AVChatRoom'){
        $url = $this->getUrl('create_group');

        if ($group_id !== 0) $params['GroupId'] = strval($group_id);
        $params['Name'] = $name;
        $params['Type'] = $type;
        $params['Owner_Account'] = strval($ownerAccount);
        $res = Http::post($url, json_encode($params));

        $res = json_decode($res, true);

        if ($res['ErrorCode'] === 0){
            return ['group_id' => $res['GroupId'], 'code' => 0];
        }
        return ['group_id' => $res['GroupId'], 'code' => -1, 'ErrorInfo' => $res['ErrorInfo']];
    }

    public function destroyGroup($groupId){
        $url = $this->getUrl('destroy_group');

        $params['GroupId'] = strval($groupId);
        $res = Http::post($url, json_encode($params));

        $res = json_decode($res, true);

        if ($res['ErrorCode'] === 0){
            return true;
        }
        return $res['ErrorInfo'];
    }

    public function destroyAllGroup(){
        $groupList = $this->getGroupList();
        foreach ($groupList as $group){
            $this->destroyGroup($group['GroupId']);
        }
    }

    public function getGroupInfo($groupIds){
        $url = $this->getUrl('get_group_info');

        if (!is_array($groupIds) && !is_string($groupIds)){
            return 'groupIds type error';
        }
        if (is_string($groupIds)){
            $groupIds = explode(',', $groupIds);
        }
        foreach ($groupIds as $id){
            $params['GroupIdList'][] = strval($id);
        }
        $res = Http::post($url, json_encode($params));

        $res = json_decode($res, true);

        if ($res['ErrorCode'] === 0){
            return $res['GroupInfo'];
        }
        return $res['ErrorInfo'];
    }


}